/**
 * Created by Administrator on 2015/6/9.
 */
var http=require('http');
var util = require('util');
var httpProxyServer= http.createServer(function(request,response){
    var remoteAddress="118.244.68.21";
    var port="80";
    console.log(request.url);
    //response.write("hello word");
    //response.end();
    var httpProxy_request=http.request({
       'host':remoteAddress,
       'port':port,
       'method':request.method,
       'path':request.url,
       'header':request.header
    });

    //httpProxy_request.setTimeout(120000,function(){
    //    httpProxy_request.abort();
    //    response.end();
    //});
    //httpProxy_request.on('response',function(proxy_response){
    //    proxy_response.on('data',function(chunk){
    //        console.info(chunk);
    //       response.write(chunk,'binary');
    //    });
    //    proxy_response.on('end',function(){
    //        response.end();
    //    });
    //    response.writeHead(proxy_response.statusCode,proxy_response.header);
    //});
    //httpProxy_request.on('error',function(err,socket){
    //
    //});


    //request.on('data',function(chunk){
       // console.info(chunk);
       // httpProxy_request.write(chunk,'binary');
   // });

});
//console.log(util.format('Http proxy server listen in %d',8383));
//httpProxyServer.on('clientError',function(err,socket){
//    console.error(util.format('Client request error: %s',err));
//});
//
//httpProxyServer.on('error',function(err,socket){
//    console.error(util.format('Client request error: %s',err));
//});
//httpProxyServer.listen(8383);

var test=function(){
    //var remoteAddress="183.128.161.45";
    var remoteAddress="120.197.234.164";
    var port="80";
    //console.log(request.url);
    //response.write("hello word");
    //response.end();
    var httpProxy_request=http.request({
        'host':remoteAddress,
        'port':port,
        'method':'GET',
        'path':"http://www.dianping.com/shop/16921202",
        'headers':{
            'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            //'accept-encoding':'gzip',
            'accept-language':'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
            'connection' :'close',
            'referer':'http://www.dianping.com/search/category/7/50/g149p23',
            'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like NeoCrawler) Chrome/31.0.1650.57 Safari/537.36'
        }
    }
    //    ,function(rep){
    //    console.info("hahah:"+rep.statusCode);
    //    rep.on('data',function(chunk){
    //        console.info(chunk);
    //    }).on('end',function(){
    //
    //    }).on('error',function(err,socket){
    //        console.info(err);
    //    });
    //}
    );
    httpProxy_request.addListener('response',function(pro_rep){
        console.log(pro_rep);
        pro_rep.addListener('data',function(chunk){
            console.info(chunk);
        });
        pro_rep.addListener('end',function(){

        });
    });
    httpProxy_request.addListener('error',function(err,socket){
        console.info('Remote proxy error: '+err);
        httpProxy_request.abort();
    });
    httpProxy_request.end();

};
test();